package com.shuaigequtan.loadbalancer.controller;


import com.shuaigequtan.result.MsgResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/consumer")
public class ConsumerCtrol {
    private static final String PAYMENTURL = "https://CLOUD-PAYMENT-SERVICE";
    private static final String PAYMENTURL2 = "https://CLOUD-PAYMENT-SERVICE1";
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WebClient.Builder clientBuilder;

    @GetMapping("/getPayment/{id}")
    public MsgResponseBody getPaymet(@PathVariable("id") Long id) {
        System.out.println("adada");
        return restTemplate.getForObject(PAYMENTURL2 + "/payment/getPayment/" + id + "?userName=123", MsgResponseBody.class);
    }

    @GetMapping("/getPayment/webClient/{id}")
    public Mono<MsgResponseBody> getPaymetByWebClient(@PathVariable("id") Long id) {
        System.out.println("bbbbbbb");
        //return restTemplate.getForObject(PAYMENTURL + "/payment/getPayment/" + id + "?userName=123", MsgResponseBody.class);
        return clientBuilder.baseUrl(PAYMENTURL).build().get().uri("/payment/getPayment/" + id + "?userName=123").retrieve().bodyToMono(MsgResponseBody.class);
    }
}
